﻿using System;
using System.Collections.Generic;
using System.Text;

class ConsoleJustification
{
    static void Main()
    {

        Queue<string> outputLineWords = new Queue<string>(); //една опашка за текста, който ще вкарваме
        Queue<string> words = new Queue<string>();         //опашка за думите в текста
                
        int linesCount = int.Parse(Console.ReadLine()); //the total number of lines in the initial text
        int maxLineLength = int.Parse(Console.ReadLine()); //the number of symbols, of which each of the justified text lines must consist

        string inputLine = Console.ReadLine();
        int inputLineNumber = 1;
        
        ExtractWordsFromLine(inputLine, words);

        int currentWordsLength = 0;
        int gapSize = 0;
        int additionalWhiteSpacesNeeded = 0;
        bool singleWordOnLine = false;
        bool shouldPrint = false;

        StringBuilder output = new StringBuilder();
        //string currentOutputLine = String.Empty;
        string currentOutputWord = String.Empty;
        string currentWord = String.Empty;

        //започваме да четем   
        while (words.Count > 0 || outputLineWords.Count > 0) //проверяваме дали не сме изпразнили опашката от думи
        {
            if (words.Count > 0)
            {
                currentWord = words.Peek();  //взимаме първата дума от опашката

                if (currentWordsLength + outputLineWords.Count + currentWord.Length <= maxLineLength)    //1 - за space
                {
                    outputLineWords.Enqueue(words.Dequeue());
                    currentWordsLength += currentWord.Length;

                    if (currentWordsLength + outputLineWords.Count - 1 == maxLineLength)
                    {
                        shouldPrint = true;
                    }
                    else
                    {
                        shouldPrint = false;
                    }
                }
                else
                {
                    shouldPrint = true;
                    //maxLineLength -(currentWordsLength + outputLineWords.Count - 1)
                    //текущите символи са currentWordsLength.  Спейсовете са (outputLineWords.Count - 1)
                    int symbolsRemaining = maxLineLength - currentWordsLength - outputLineWords.Count + 1;    //намираме колко space-а ни остават
                    //как да разделим space-вете между думите
                    if (outputLineWords.Count > 1)
                    {
                        singleWordOnLine = false;
                        gapSize = symbolsRemaining / (outputLineWords.Count - 1);   //ще гърми когато имаме 1 дума на ред
                        additionalWhiteSpacesNeeded = symbolsRemaining % (outputLineWords.Count - 1);
                    }
                    else
                    {
                        //only one word on line
                        singleWordOnLine = true;
                    }
                }      
            }
            else
            {
                shouldPrint = true;
                //maxLineLength -(currentWordsLength + outputLineWords.Count - 1)
                //текущите символи са currentWordsLength.  Спейсовете са (outputLineWords.Count - 1)
                int symbolsRemaining = maxLineLength - currentWordsLength - outputLineWords.Count + 1;    //намираме колко space-а ни остават
                //как да разделим space-вете между думите
                if (outputLineWords.Count > 1)
                {
                    singleWordOnLine = false;
                    gapSize = symbolsRemaining / (outputLineWords.Count - 1);   //ще гърми когато имаме 1 дума на ред
                    additionalWhiteSpacesNeeded = symbolsRemaining % (outputLineWords.Count - 1);
                }
                else
                {
                    //only one word on line
                    singleWordOnLine = true;
                }
            }
                 

            if (shouldPrint)
            {
                if (singleWordOnLine)
                {
                    currentOutputWord = outputLineWords.Dequeue();
                    output.Append(currentOutputWord);
                    singleWordOnLine = false;                    
                }
                else
                {
                    while (outputLineWords.Count > 1)       //когато имаме повече от 1 думи
                    {
                        currentOutputWord = outputLineWords.Dequeue();
                        output.Append(currentOutputWord);
                        output.Append(new String(' ', gapSize + 1)); //и сега трябва да добавим space-вете

                        if (additionalWhiteSpacesNeeded > 0)
                        {
                            output.Append(' ');
                            additionalWhiteSpacesNeeded--;
                        }
                    }
                    currentOutputWord = outputLineWords.Dequeue();

                    output.Append(currentOutputWord);
                }

                output.AppendLine();
                currentWordsLength = 0;
                gapSize = 0;
                additionalWhiteSpacesNeeded = 0;
            }
            
            //Output
            if (words.Count == 0 && inputLineNumber < linesCount)
            {
                inputLine = Console.ReadLine();
                inputLineNumber++;
                ExtractWordsFromLine(inputLine, words);                
            }            
        }
        Console.Write(output.ToString());
    }
    
    private static void ExtractWordsFromLine(string inputLine, Queue<string> words)
    {
        string[] wordsOnLine = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        //string[] wordsOnLine = Regex.Split(inputLine, @"\s+");
        for (int index = 0; index < wordsOnLine.Length; index++)
        {
            string word = wordsOnLine[index];
            if (word != String.Empty)
            {
                words.Enqueue(word);
            }
        }
    }
}

